import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
/**
 * 
 *is the control class of Update Book Subject window.
 */
public class UpdateBookSubjectCTRL
{
	/** array of strings for data sent to database */
	public String[] searchData3 = new String[3];
	public String[] searchData2 = new String[3];
	/** array of strings for result*/
	public String[] res;
	/** string for Book's name */
	public String BookName;
	
	/**
	 * this method collects needed info from gui, and sends the DB a request to add the
	 *	 entered Subject+Field to the selected book. (if already exists notify to user)
	 *   after completion, also reset gui to its initial state
	 */
	public void o_AddSubject(UpdateBookSubject updtbookSubGui)
	{
		String temp;
		Main.welcomeScreen.SubjectsData = new String[5];
		Main.welcomeScreen.SubjectsData[0]=Main.gusers.id;
		Main.welcomeScreen.SubjectsData[1]="AddSubjectForBook";	
		Main.welcomeScreen.SubjectsData[2]=(String)Main.UpdBooksSubjectGui.FieldCombo2.getSelectedItem();
		Main.welcomeScreen.SubjectsData[3]=(String)Main.UpdBooksSubjectGui.SubjectCombo2.getSelectedItem();
		Main.welcomeScreen.SubjectsData[4]=Main.UpdBooksSubjectGui.BookSerial;
		Main.log.connection.accept(Main.welcomeScreen.SubjectsData[1]);
		Main.log.flag=false;
		while(Main.log.flag==false);
		Main.log.flag=false;
		temp=ChatClient.getRs();
		if(temp.equals("Exists"))
			JOptionPane.showMessageDialog(null,"The Selected Field & Subject : '"+Main.welcomeScreen.SubjectsData[2]+", "+ Main.welcomeScreen.SubjectsData[3]+"'  already exists for this book!","Database Conflict",JOptionPane.ERROR_MESSAGE);
		else if(temp.equals("OK"))
		{
			JOptionPane.showMessageDialog(null,"Field & Subject Succesfully added to the Book in Database","Database Success",JOptionPane.INFORMATION_MESSAGE);
			RefreshSubjects(updtbookSubGui);
			Main.UpdBooksSubjectGui.FieldCombo2.setVisible(false);
			Main.UpdBooksSubjectGui.ClearSel.setVisible(false);
			Main.UpdBooksSubjectGui.add.setEnabled(false);
		}			
	}
	/**
	 * this method collects needed info from gui, and sends the DB a request to Remove the
	 *	 entered Subject+Field from the selected book. 
	 *   after completion, also reset gui to its initial state
	 */
	public void o_RemoveSubject(UpdateBookSubject updtbookSubGui)
	{		
		String[] TempArr,TempArr2;
		Main.welcomeScreen.SubjectsData = new String[5];
		Main.welcomeScreen.SubjectsData[0]=Main.gusers.id;
		Main.welcomeScreen.SubjectsData[1]="RemoveSubjectForBook";			
		Main.welcomeScreen.SubjectsData[4]=Main.UpdBooksSubjectGui.BookSerial;
		if (updtbookSubGui.SubjectsList.isSelectionEmpty() )
				JOptionPane.showMessageDialog(null,"Please Select a subject from list. \n (If list is empty,first add subjects) ","Database Success",JOptionPane.ERROR_MESSAGE);
		else
		{
			TempArr=((String)updtbookSubGui.SubjectsList.getSelectedValue()).split("\\:");
			TempArr2=TempArr[0].split("\\ ");
			Main.welcomeScreen.SubjectsData[2]=TempArr2[3];		// Field
			TempArr2=TempArr[1].split("\\ ");
			Main.welcomeScreen.SubjectsData[3]=TempArr2[2];		// Subject
			Main.log.connection.accept(Main.welcomeScreen.SubjectsData[1]);
			Main.log.flag=false;
			while(Main.log.flag==false);
			Main.log.flag=false;
			ChatClient.getRs();		
			JOptionPane.showMessageDialog(null,"Field & Subject Succesfully Removed from Book in Database","Database Success",JOptionPane.INFORMATION_MESSAGE);
			RefreshSubjects(updtbookSubGui);
		}
	}
	
	/**
	 * this method Refreshes the gui to its initial state after adding/removing
	 */
	void RefreshSubjects(UpdateBookSubject updtbookSubGui)
	{
		updtbookSubGui.panel.remove(updtbookSubGui.SubjectsList);
		updtbookSubGui.SubjectsList=null;
		Main.UpdBooksSubjectGui.Arr=o_GetBooksSubjects(Main.UpdBooksSubjectGui.BookSerial);
		Main.UpdBooksSubjectGui.panel.add(Main.UpdBooksSubjectGui.getSubjectsList(), null);
		Main.UpdBooksSubjectGui.SubjectsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION );
		Main.UpdBooksSubjectGui.SubjectCombo2.setEnabled(true);
		Main.UpdBooksSubjectGui.ViewFields.setEnabled(true);		
		Main.UpdBooksSubjectGui.panel.validate();
		Main.UpdBooksSubjectGui.panel.repaint();
	}
	
	/**
	 * this method collects needed info from gui, and sends the DB a request to retreive the
	 *	 wanted Subject+Field for the selected book. 
	 *   returns a strings array, each cell in format : "Field:Subject"
	 */
	public String[] o_GetBooksSubjects(String serial) 
	{
		String temp,Ares[],Bres[];	 
		Main.welcomeScreen.SubjectsData = new String[3];
		Main.welcomeScreen.SubjectsData[0]=Main.gusers.id;
		Main.welcomeScreen.SubjectsData[1]="GetBooksSubjects";	
		Main.welcomeScreen.SubjectsData[2]=serial;			
		Main.log.connection.accept(Main.welcomeScreen.SubjectsData[1]);
		Main.log.flag=false;
		while(Main.log.flag==false);
		Main.log.flag=false;
		temp=ChatClient.getRs();
		Ares=temp.split("\\*");
		BookName=Ares[0];
		Bres= new String[Ares.length-1];
		for(int i=1; i<(Ares.length); i++)
			Bres[i-1]=Ares[i];		
		return Bres;
	}
	/**
	 * this method retreives the Subject list for the combo box
	 *   returns a strings array, each cell in format : "Subject"
	 */
	public String[] o_GetSubjects() 
	{
		String temp;
		Main.welcomeScreen.SubjectsData=new String[2];
		Main.welcomeScreen.SubjectsData[0]=Main.gusers.id;
		Main.welcomeScreen.SubjectsData[1]="getSubjects1";				
		Main.log.connection.accept(Main.welcomeScreen.SubjectsData[1]);
		Main.log.flag=false;
		while(Main.log.flag==false);
		Main.log.flag=false;		
		temp=ChatClient.getRs();
		res=temp.split("\\*");	
		return res;
	}
	/**
	 * this method retreives the Fields list for the Selected Subject ,
	 *   returns a strings array, each cell in format : "Field"
	 */
	public String[] o_GetFields() 
	{
		String temp,Ares[];	 
		Main.welcomeScreen.SubjectsData = new String[3];
		Main.welcomeScreen.SubjectsData[0]=Main.gusers.id;
		Main.welcomeScreen.SubjectsData[1]="getFieldsSubject1";	
		Main.welcomeScreen.SubjectsData[2]=(String)Main.UpdBooksSubjectGui.SubjectCombo2.getSelectedItem();			
		Main.log.connection.accept(Main.welcomeScreen.SubjectsData[1]);
		Main.log.flag=false;
		while(Main.log.flag==false);
		Main.log.flag=false;
		temp=ChatClient.getRs();
		Ares=temp.split("\\*");
		return Ares;
	}		
	/**
	 * this method Goes back to the last Gui Screen - Book Management
	 */	
	public void o_Back(JPanel panel) 
	{
		panel.setVisible(false);
		panel.setEnabled(false);
		Main.log.frame.setSize(530,340);
		Main.bookManagement.panel.setVisible(true);
		Main.bookManagement.panel.setEnabled(true);		
	}
	/**
	 * this method logs out of the system
	 */	
	public void o_logout(JPanel BookMngpanel)
	{
		if(JOptionPane.showConfirmDialog(null, "Are you sure you want to logout? ","Confirmation" ,JOptionPane.OK_CANCEL_OPTION) == 0)
		{
			BookMngpanel.setEnabled(false);
			BookMngpanel.setVisible(false);
			Main.log.loginData = new String[3];
			Main.log.loginData[0]=Main.gusers.id;
			Main.log.loginData[1]="logout";
			Main.log.loginData[2]=Main.gusers.id;			
			Main.log.connection= new ClientQuery(Main.serverIP,5555);
			Main.log.connection.accept("logout");
			Main.log.user.setText("");
			Main.log.pass.setText("");
			Main.log.frame.setSize(650,550);
			Main.log.panel.setEnabled(true);
			Main.log.panel.setVisible(true);
		}
	}

}

